﻿@using EmployeeLeaveSystem.Shared;
@using EmployeeLeaveSystem.Shared.Models;
@inject IMessageService _MessageService;
@inject HttpClient _HttpClient;

<Form Model="addMoveDepartment"
      Layout="@FormLayout.Vertical"
      OnFinish="UpLoad">
    <FormItem Label="姓名">
        @GobalManager.myInfo?.Name
    </FormItem>
    <FormItem Label="意愿部门">
        <Select @bind-DataSource="departments"
                TItem="Department"
                TItemValue="int?"
                @bind-Value="context.ToDepartmentId"
                LabelName="@nameof(Department.Name)"
                ValueName="@nameof(Department.Id)"
                Placeholder="选择一个部门"
                Loading="@_SelectLoading"
                OnFocus="@SelectFocus">
        </Select>
    </FormItem>
    <FormItem Label="原因">
        <TextArea @bind-Value="context.Reason" />
    </FormItem>
    <FormItem Label="附件上传">
        <Upload Action="/Filesave"
                Name="files"
                Drag
                Multiple
                OnSingleCompleted="OnSingleCompleted">
            <p class="ant-upload-drag-icon">
                <Icon Type="inbox" />
            </p>
            <p class="ant-upload-text">点击或者拖着文件到此区域来上传文件</p>
            <p class="ant-upload-hint"> 支持一个或者多个文件的上传，上传完成后文件会保存在服务器当中</p>
        </Upload>

        </FormItem>
        <FormItem Label="上传日期">
        @DateTime.Now.ToString("yyyy-MM-dd")
        </FormItem>

        <FormItem>
        <Button Block Type="@ButtonType.Primary" HtmlType="submit">
            提交
        </Button>
        </FormItem>

        </Form>





        @code {
    AddMoveDepartment addMoveDepartment = new();
    List<Department> departments = new();
    List<UploadFileItem>? fileLists;
    void OnSingleCompleted(UploadInfo fileinfo) {
        fileLists = fileinfo.FileList;
        if (fileinfo.File.State == UploadState.Success) {
            var result = fileinfo.File.GetResponse<ResponseModel>();
            fileinfo.File.Url = result.url;
        }
    }
    protected override async Task OnInitializedAsync() {
        addMoveDepartment.StaffId = GobalManager.myInfo?.Id;
    }

    // SELECT 选择器
    bool _SelectLoading = false;
    async void SelectFocus() {
        _SelectLoading = true;
        _MessageService.Info("加载部门中");
        departments = await _HttpClient.GetFromJsonAsync<List<Department>>("api/Department/GetAllDepartmentInfos");
        _MessageService.Success("加载完成");
        _SelectLoading = false;
        InvokeAsync(StateHasChanged);
    }

    // 文件上传
    async void UpLoad() {
        _MessageService.Info("上传中");

        addMoveDepartment.Files.Clear();
        if (fileLists != null) {
            foreach (var it in fileLists) {
                addMoveDepartment.Files.Add(new() { StaffId = GobalManager.myInfo?.Id, FileName = it.FileName });
            }
        }

        await _HttpClient.PostAsJsonAsync<AddMoveDepartment>("api/Move/AddMoveInfo", addMoveDepartment);

        _MessageService.Info("上传完毕");
    }
}
